//
//  OIRegisterViewController.m
//  EducationOID
//
//  Created by slz on 2017/5/23.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "OIRegisterViewController.h"
#import "OILineTextField.h"
#import "OILoginViewController.h"
#import "YWStaticLoader.h"

@interface OIRegisterViewController ()
@property (weak, nonatomic) IBOutlet OILineTextField *uAccountTextFiled;
@property (weak, nonatomic) IBOutlet OILineTextField *uPwdTextFiled;
@property (weak, nonatomic) IBOutlet OILineTextField *uEamilTextFiled;
@property (weak, nonatomic) IBOutlet UIButton *regitsterButton;

@end

@implementation OIRegisterViewController
{
    YWStaticLoader * loader;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setControllerTitle:@"注册"];
    self.showNavigationBar = YES;
    self.view.backgroundColor = kBack_Color_Default;
    UIBarButtonItem * item = [UIBarButtonItem itemWithImage:@"top_back_1" selectImage:nil addTarget:self andAction:@selector(backAction)];
    self.navigationItem.leftBarButtonItem = item;

    self.regitsterButton.layer.cornerRadius = 5;
    // Do any additional setup after loading the view from its nib.
}

- (IBAction)registerAction:(id)sender {
    BOOL shouldRequest = NO;
    NSString * errorInfoString = [self shouldSendRegisterRequest:&shouldRequest];
    if (!shouldRequest){
        [self showErrorMsg:errorInfoString];
        return;
    }
    [self showProgress:@"正在注册..."];
    [[MFWUserLoginManager sharedInstance]registerWithOIDUserName:_uAccountTextFiled.text password:_uPwdTextFiled.text email:_uEamilTextFiled.text finisheHandler:^(BOOL success, MFWUserInfo *userInfo, NSError *error) {
        [self hideProgress];
        if (success){
            [self showTipAniamtionDuration:2 comletoion:^{
                [self dismissRegisterController];
            }];
        }else{
            [self showErrorMsg:[error.userInfo stringValueForKey:@"errorMsg"]];
        }
    }];
}

- (void)showTipAniamtionDuration:(NSInteger)duration comletoion:(void(^)())comletion{
        [self.view showTip:@"注册成功！" completion:^{
            if (comletion){
                comletion();
            }
        }];
}

- (void)showErrorMsg:(NSString *)errorMsg{
    [self.view showTip:errorMsg];
}

- (void)dismissRegisterController{
//    ((OILoginViewController*)self.navigationController.viewControllers[1]).dismiss();
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSString *)shouldSendRegisterRequest:(BOOL *)shouldRequest{
    NSString * error;
    if ([MFWStringUtils isEmptyString:_uAccountTextFiled.text] || [MFWStringUtils isEmptyString:_uPwdTextFiled.text]){
        *shouldRequest = NO;
        error = @"账号或密码不能为空";
    }else if (![self validateEmail:_uEamilTextFiled.text]){
        *shouldRequest = NO;
        error = @"请输入正确的邮箱地址";
    }else{
        *shouldRequest = YES;
    }
    return error;
}

-(void)hideProgress
{
    if(loader) {
        [loader hidden];
        loader = nil;
    }
}

-(void)showProgress:(NSString*)msg{
    if (loader==nil){
        loader = [[YWStaticLoader alloc]initWithLoadingMsg:msg];
        [loader showInView:self.navigationController.view isRoot:NO];
    } else {
        [loader setLoadingMsg:msg];
    }
}

//check email
- (BOOL)validateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
